define(['app'],function(app){return {module: function($scope, $http, eventBusService, httpService, params) {
	$scope.dataInfo = {};
		$scope.dataList = [];
	$scope.form.fk_unit='self';
	httpService.post(app.urls.menu_select,{"id":params.father_id},function(data){
		if("0000" === data.code){
			$scope.menu_name = data.data[0].name;
			$scope.findButton(data.data[0].id);
		}else{
			alert(data.msg);
		}
	});
	$scope.findButton=function(menu_id){
		httpService.post(app.urls.button_select,{"menu_id":menu_id},function(data){
			if("0000" === data.code){
				$scope.buttons = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	//查询学年学期
	httpService.post(app.urls.prop_select,{"prop_group":'SCORE',"prop_key":'STU_YEAR'},function(data){
		if("0000" === data.code){
			$scope.academics =  data.data;
		}else{
			alert(data.msg);
		}
	});
	//查询学期
	httpService.post(app.urls.prop_select,{"prop_group":'SCORE',"prop_key":'STU_TERM'},function(data){
		if("0000" === data.code){
			$scope.semesters = data.data;
		}else{
			alert(data.msg);
		}
	});
	$scope.findGrade = function() {
		if($scope.dataInfo.fk_unit == null) {
			$scope.form.fk_unit = "self";
		}else{
			$scope.form.fk_unit = $scope.dataInfo.fk_unit;
		}
		httpService.post(app.urls.grade_selectCenterGrade,{"fk_unit":$scope.form.fk_unit},function(data){
			if("0000" === data.code){
				$scope.gradeList = data.data;
			}else{
				alert(data.msg);
			}
		});
		if($scope.unitList.length == 1){
			$scope.form.weekunit = $scope.unitList[0].week;
		}else{
			for(var i=0;i<$scope.unitList.length;i++){
				if($scope.unitList[i].id == $scope.form.fk_unit){
					$scope.form.weekunit = $scope.unitList[i].week;
				}
			}
		}
		if($scope.form.weekunit == '0'){
			$scope.form.week = '0';
		}
		if($scope.form.weekunit == '1'){
			$scope.form.week = '';
		}
	}
	//查询主校区和分校区
	httpService.post(app.urls.unit_selectSchoolAndAttach,{"fk_unit":$scope.form.fk_unit},function(data){
		if("0000" === data.code){
			$scope.unitList = data.data;
			if($scope.unitList.length==1){
				$scope.findGrade();
			}
		}else{
			alert(data.msg);
		}
	});
	//获取班级
	$scope.findClass=function(){
		httpService.post(app.urls.class_selectClassName,$scope.form,function(data){
			if("0000" === data.code){
				$scope.classList = data.data;
			}else{
				alert(data.msg);
			}
		});
	}
	var init =function(){
		//查询当前学年学期
		httpService.post(app.urls.prop_select,{"prop_group":'SCORE','status':'0'},function(data){
			if("0000" === data.code){
				for(var i=0;i<data.data.length;i++){
					if(data.data[i].prop_key=='STU_YEAR'){
						$scope.form.academic =  data.data[i].prop_pk;
					}
					if(data.data[i].prop_key=='STU_TERM'){
						$scope.form.semester =  data.data[i].prop_pk;
					}
				}

			}else{
				alert(data.msg);
			}
		});
	}
	init();

	//查询课表数据
	$scope.find = function(){
		httpService.post(app.urls.course_table_selectTableByClass,$scope.form,function(data){
			if("0000" === data.code){
				$scope.dataList = data.data;
				for(var i=0;i<$scope.dataList.length;i++){
					if($scope.dataList[i].course_time!='undefined'||$scope.dataList[i].course_time!=undefined||$scope.form.fk_class==''||$scope.dataList[i].course_time!=null){
						$scope.form.time=true;
					}
				}
			}else{
				alert(data.msg);
			}
		});
	}
	$scope.update = function(){
		if($scope.form.weekunit == undefined){
			alert("请设置该校区的课表类型");
			return;
		}
		if($scope.form.fk_class=='undefined'||$scope.form.fk_class==undefined||$scope.form.fk_class==''||$scope.form.fk_class==null){
			alert("请选择班级");
			return;
		}
		if($scope.form.weekunit == '1' && ($scope.form.week=='undefined'||$scope.form.week==undefined||$scope.form.week==''||$scope.form.week==null)){
			alert("请选择单/双周");
			return;
		}
		if(!$scope.form.time){
			alert("请设置上课时间");
			return;
		}
		var academic = $scope.form.academic;
		var semester = $scope.form.semester;
		var fk_unit = $scope.form.fk_unit;
		var fk_grade = $scope.form.fk_grade;
		var fk_class = $scope.form.fk_class;
		var week = $scope.form.week;
		eventBusService.publish("modal.page","modules/content/baseinfo/coursetable/update?fk_grade="+fk_grade+"&academic="+academic+"&semester="+semester+"&fk_unit="+fk_unit+"&fk_class="+fk_class+"&week="+week);
	}

	$scope.clickBtn=function(event){
        if(event=='update'){
            $scope.update();
        }
        if(event=='courseTableScheme'){
            $scope.courseTableScheme();
        }
	}

	/**
	 * 课表方案
	 */
	$scope.courseTableScheme = function(){
		let url = "modules/content/baseinfo/coursetable/scheme";
        eventBusService.publish("modal.page", url);
	}

	//接收数据刷新
	eventBusService.subscribe('content.page.reload', function(event,data) {
		$scope.findClass();
		$scope.find();
	});
		//选择数据
		$scope.optData = function (element) {
			if($(element).find("input[name='dataPk']").is(":checked")){
				$(element).find("input[name='dataPk']").prop("checked",false);
				$(element).removeClass("opt_tr");
			}else{
				$(element).find("input[name='dataPk']").prop("checked",true);
				$(element).addClass("opt_tr");
			}
		}
		$scope.checkAll = function(target) {
			$("#" + $scope.controllerName + " input[name='dataPk']").each(function() {
				if ($(this).prop("checked")) {
					$(this).prop('checked', false);
					$(this).parents("tr").removeClass("opt_tr");
				} else {
					$(this).prop('checked', true);
					$(this).parents("tr").addClass("opt_tr");
				}
			})
		}
}}});